package com.spic.system.controller;

import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.datascope.annotation.DataScope;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import com.spic.system.domain.SysAnnounce;
import com.spic.system.domain.vo.SysAnnounceReaderVo;
import com.spic.system.service.ISysAnnounceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 发布公示公告内容Controller
 *
 * @author spic
 * @date 2022-06-15
 */
@RestController
@RequestMapping("/announce")
public class SysAnnounceController extends BaseController
{
    @Autowired
    private ISysAnnounceService sysAnnounceService;

    /**
     * 查询发布公示公告内容列表
     */
    @DataScope(deptAlias = "d", userAlias = "u", menuPath = "system/publicity/announcement/index")
    @PreAuthorize(hasPermi = "system:publicity:announcement:list")
    @GetMapping("/list")
    public TableDataInfo list(SysAnnounce sysAnnounce)
    {
        startPage();
        List<SysAnnounce> list = sysAnnounceService.selectSysAnnounceList(sysAnnounce);
        return getDataTable(list);
    }

    //未读人员
    @GetMapping(value = "/readerInfoList")
    public TableDataInfo getReaderInfo(SysAnnounceReaderVo sysAnnounceReaderVo)
    {
        startPage();
        List <SysAnnounceReaderVo>  list = sysAnnounceService.selectReaderInfoByAnnounceId(sysAnnounceReaderVo);
        return getDataTable(list);
    }
    @GetMapping("/selectCountByType")
    public AjaxResult getCountByType(){
        List <Map<String,String>> countMap =  sysAnnounceService.getCountByType();
        return AjaxResult.success(countMap);
    }
    /**
     * 导出发布公示公告内容列表
     */
//    @PreAuthorize(hasPermi = "system:announcement:export")
    @Log(title = "发布公示公告内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysAnnounce sysAnnounce) throws IOException
    {
        List<SysAnnounce> list = sysAnnounceService.selectSysAnnounceList(sysAnnounce);
        ExcelUtil<SysAnnounce> util = new ExcelUtil<SysAnnounce>(SysAnnounce.class);
        util.exportExcel(response, list, "发布公示公告内容数据");
    }

    /**
     * 获取发布公示公告内容详细信息
     */
//    @PreAuthorize(hasPermi = "system:announcement:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysAnnounceService.selectSysAnnounceById(id));
    }

    /**
     * 新增发布公示公告内容
     */
//    @PreAuthorize(hasPermi = "system:announcement:add")
    @Log(title = "发布公示公告内容", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAnnounce sysAnnounce)
    {
        sysAnnounceService.insertSysAnnounce(sysAnnounce);
        return AjaxResult.success();
    }

    /**
     * 修改发布公示公告内容
     */
//    @PreAuthorize(hasPermi = "system:announcement:edit")
    @Log(title = "发布公示公告内容", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAnnounce sysAnnounce)
    {
        sysAnnounceService.updateSysAnnounce(sysAnnounce);
        return AjaxResult.success();
    }

    /**
     * 删除发布公示公告内容
     */
//    @PreAuthorize(hasPermi = "system:announcement:remove")
    @Log(title = "发布公示公告内容", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        sysAnnounceService.deleteSysAnnounceById(id);
        return AjaxResult.success();
    }

    //广告机值班人员
    @GetMapping("/getPersonnelDuty")
    public AjaxResult getPersonnelDuty(){
        return AjaxResult.success(sysAnnounceService.getPersonnelDuty());
    }
}
